const {
  $util,
} = getApp();
Page({
  data: {
    page: 1,
    list: [],
    hasMore: true,
    ctype: 0, // 0 我的  1 管理
    navLoad: false,
    adminNav: [{
      title: '我提交的',
      id: 0,
    }, {
      title: '待我审批',
      id: 1,
    }],
    ifShowNote: true,
    learn_type: ''
  },
  onLoad(ops) {
    wx.setNavigationBarTitle({
      title: '党员学习',
    });
    Number(ops.ctype) === 1 && this.changeNav({
      detail: { obj: { id: 1 } },
    });
  },
  onShow() {
    this.setData({
      page: 1,
      list: [],
      loaded: false,
    }, () => {
      Number(this.data.ctype) === 1 ? this.loadManageList() : this.loadList();
    });
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },
  loadList() {
    const D = this.data;
    $util.request({
      url: 'entry/wxapp/edu.offlinetrain.offlist',
      data: {
        page: D.page,
      },
      success: (res) => {
        console.log(res.list)
        let list = res.list;
        if (D.page > 1) {
          list = [...D.list, ...list];
        }
        this.setData({
          list,
          canshow: res.canshow,
          navLoad: true,
          hasMore: parseInt(res.total, 0) > list.length,
          loaded: true,
        });

        for (var i in res.list) {
          // 判断是否隐藏上传笔记
          if (res.list[i].upload_notes == 0) {
            this.setData({
              ifShowNote: false
            })
          } else if (res.list[i].upload_notes == 1) {
            this.setData({
              ifShowNote: true
            })
          }
          // 显示学习分类
          if (res.list[i].learn_type == 2) {
            this.setData({
              learn_type: "自学"
            })
          } else if (res.list[i].learn_type == 1) {
            this.setData({
              learn_type: "集中学习"
            })
          }
        }
      },
    });
  },

  /* 审批培训笔记 */
  loadManageList() {
    let {
      page,
      list,
    } = this.data;
    $util.request({
      url: 'entry/wxapp/verify.offrecord.getList',
      data: {
        check: 0,
        page: page,
      },
      success: (res) => {
        let lists = res.list;
        if (page > 1) {
          lists = [...list, ...lists];
        }
        this.setData({
          hasMore: res.list.length !== 0,
          list: lists,
          navLoad: true,
          canshow: true,
          loaded: true,
        });
      },
      fail: (err) => {
        console.error(err);
      },
    });
  },

  // 是否隐藏上传笔记

  onReachBottom() {
    let { ctype, hasMore } = this.data;
    if (!hasMore) return;
    this.setData({
      page: this.data.page + 1,
    }, () => {
      Number(ctype) === 0 ? this.loadList() : this.loadManageList();
    });
  },

  changeNav(e) {
    this.setData({
      page: 1,
      ctype: Number(e.detail.obj.id),
      loaded: false,
      list: [],
      hasMore: true,
    }, () => {
      if (this.data.canshow && e.detail.obj.id === 0) {
        this.loadList();
      } else {
        this.loadManageList();
      }
    });
  },
});
